<?php

namespace common\models\goods;

use Yii;

/**
 * 这个模型基于表 "{{%goods_attr_val}}" 生成。
 *
 * @property integer $id
 * @property integer $goods_id
 * @property integer $attr_id
 * @property string $attr_value
 * @property string $aliases
 * @property string $img_url
 */
class GoodsAttrVal extends \common\models\AR
{
    public $modelName='';
    
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%goods_attr_val}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['goods_id', 'attr_id', 'attr_value'], 'required'],
            [['goods_id', 'attr_id'], 'integer'],
            [['attr_value'], 'string', 'max' => 45],
            [['aliases', 'img_url'], 'string', 'max' => 80]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'goods_id' => 'Goods ID',
            'attr_id' => '商品属性ID',
            'attr_value' => '系统默认 商品属性值',
            'aliases' => '商家自定义别名',
            'img_url' => '商家上传的商品属性图片',
        ];
    }
        
    public function afterDelete() {
        parent::afterDelete();
        if(!empty($this->img_url)){
            $filePath=Yii::getAlias('@img') . "/" . $this->img_url;
            @unlink($filePath);
        }
    }
    
    public function saveImg($tmpPath){
        if (empty($tmpPath)) {
            return FALSE;
        }

        $ext = pathinfo($tmpPath, PATHINFO_EXTENSION);
        $newFileName = uniqid().'.' . $ext;
        $formPath = Yii::getAlias('@temp') . '/' . $tmpPath;
        $t = \backend\models\Supplier::getImgPath('goodsAttr') . '/' . $newFileName;
        $newPath = Yii::getAlias('@img') . "/" . $t;

        $result=@rename($formPath, $newPath);
        if ($result) {
            $this->img_url = $t;
        }
        return $result;
    }

}
